﻿using Hxj.Data;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Client.BLL.Impl
{
    public class BLLConsult
    {
        DAL.DALConsult dal = new DAL.DALConsult();
        #region 获取 待回复问题列表
        public DataTable GetWaitResponseProblemTable(string strSignUpID, string strIsTop, string strIsStudent, string strIsFee, string strConsultResorce)
        {
            string strCondition = "";
            if (!strSignUpID.IsNullOrEmptyString())
            {
                strCondition += " and  B.NickName ='" + strSignUpID + "' ";
            }
            if (!strIsTop.IsNullOrEmptyString() && strIsTop.ToSafeBool())
            {
                strCondition += " and  A.IsTop = '" + strIsTop + "' ";
            }
            if (!strIsStudent.IsNullOrEmptyString() && strIsStudent.ToSafeBool())
            {
                strCondition += " and  B.UserType = '考生' ";
            }
            if (!strIsFee.IsNullOrEmptyString() && strIsFee.ToSafeBool())
            {
                strCondition += " and  B.IsFee = '" + strIsFee + "' ";
            }
            if (!strConsultResorce.IsNullOrEmptyString())
            {
                strCondition += " and  A.ConsultResorce = '" + strConsultResorce + "' ";
            }

            return dal.GetWaitResponseProblemList(strCondition);
        }
        #endregion

        #region 获取 已回复问题列表
        public DataTable GetRepliedProblemTable(string strSignUpID, string strIsTop, string strIsFee, string strIsStudent,
            string strConsultResorce, string strConsultTitle, string strUsersNickName)
        {
            string strCondition = "";
            if (!strSignUpID.IsNullOrEmptyString())
            {
                strCondition += " and  B.NickName ='" + strSignUpID + "' ";
            }
            if (!strIsTop.IsNullOrEmptyString()&& strIsTop.ToSafeBool())
            {
                strCondition += " and  A.IsTop = '1' ";
            }
            else
            {
                strCondition += " and  ISNULL(A.IsTop,'0') = '0' ";
            }
            if (!strIsFee.IsNullOrEmptyString() && strIsFee.ToSafeBool())
            {
                strCondition += " and  B.IsFee = '1' ";
            }
            else
            {
                strCondition += " and  ISNULL(B.IsFee,'0') = '0' ";
            }
            if (!strIsStudent.IsNullOrEmptyString() && strIsStudent.ToSafeBool())
            {
                strCondition += " and  B.UserType = '考生' ";
            }
            else
            {
                strCondition += " and  ISNULL(B.UserType,'') <> '考生' ";
            }
            if (!strConsultResorce.IsNullOrEmptyString())
            {
                strCondition += " and  A.ConsultResorce like '%" + strConsultResorce + "%' ";
            }
            if (!strConsultTitle.IsNullOrEmptyString())
            {
                strCondition += " and  A.ConsultTitle like '%" + strConsultTitle + "%' ";
            }
            if (!strUsersNickName.IsNullOrEmptyString())
            {
                strCondition += " and  A.UsersNickName like '%" + strUsersNickName + "%' ";
            }
            return dal.GetRepliedProblemList(strCondition);
        }

        #endregion

        #region 获取 回复区展示
        public DataTable GetReplyAreaShow(string strTopicID)
        {
            string sql = @"  select
                                    A.ID,
                                    '【' + A.ConsultTypeName + '】 '  + A.ConsultTitle AS ConsultTitle,
                                    A.ConsultContent,
                                    A.CreateDate,
                                    A.UsersNickName,
                                    B.RConsultContent,
                                    B.CreateDate AS RCreateDate,
                                    B.RConsultUsersNickName,
                                    B.IsQuestions,
                                    A.State
                                from	  TB_ConsultTitle A              
                                left join TB_ConsultReply B ON A.ID = B.TopicID                            
                                WHERE 1=1 AND  B.TopicID = '{0}' 
                                ORDER BY A.CreateDate,B.CreateDate
                            ";

            sql = string.Format(sql, strTopicID);
            return dal.GetTable(sql);
        }
        #endregion

        #region 保存咨询互动回复表 更新咨询互动标题表
        public JsonMessage SaveConsultReply_UpdateConsultTitle(string strMsgID, string strRichSend, string strUserName, string strSend, string strUserID, string strZX)
        {
            JsonMessage message = new JsonMessage();

            try
            {
                string sql = @"
                                insert into TB_ConsultReply
                                select newid(),'{0}','','{1}','{3}',0,getdate(),'{4}','{2}','127.0.0.1','1','0','{5}'

                                update TB_ConsultTitle 
                                SET IsReply = '1',
                                    ReplyDate =  GETDATE(),
                                    ReadCount = ReadCount + 1
                                where ID = '{0}'
                            ";

                sql = string.Format(sql, strMsgID, strRichSend, strUserName, strSend, strUserID, strZX);
                dal.ExecuteNonQuery(sql);
                message.State = "true";
                message.Msg = "保存成功!";
            }
            catch (Exception ex)
            {
                message.State = "false";
                message.Msg = "保存失败," + ex.Message;
            }
            return message;
        }
        #endregion

        #region  设置不公开
        public JsonMessage UpdateIsPublic(string strMsgID, string strIsPublic)
        {
            JsonMessage message = new JsonMessage();
            try
            {
                string sql = @" update TB_ConsultTitle
                            SET IsPublic = '{1}' 
                            where ID = '{0}'
                        ";
                sql = string.Format(sql, strMsgID, strIsPublic);
                dal.ExecuteNonQuery(sql);
                message.State = "true";
                message.Msg = "设置成功!";
            }
            catch (Exception ex)
            {
                message.State = "false";
                message.Msg = "设置失败," + ex.Message;
            }
            return message;
        }
        #endregion

        #region  删除咨询互动列表
        public JsonMessage DeleteConsultTitle(string strMsgID)
        {
            JsonMessage message = new JsonMessage();
            try
            {
                string sql = @"
                            update TB_ConsultTitle
                            SET State = '0' 
                            where ID = '{0}'
                        ";
                sql = string.Format(sql, strMsgID);
                dal.ExecuteNonQuery(sql);
                message.State = "true";
                message.Msg = "删除成功!";
            }
            catch (Exception ex)
            {
                message.State = "false";
                message.Msg = "删除失败," + ex.Message;
            }
            return message;
        }
        #endregion

        #region  设置置顶
        public JsonMessage UpdateIsTop(string strMsgID, string strIsTop)
        {
            JsonMessage message = new JsonMessage();
            try
            {
                string sql = @"
                            update TB_ConsultTitle
                            SET IsTop = '{1}' 
                            where ID = '{0}'
                        ";
                sql = string.Format(sql, strMsgID, strIsTop);
                dal.ExecuteNonQuery(sql);
                message.State = "true";
                message.Msg = "设置成功!";
            }
            catch (Exception ex)
            {
                message.State = "false";
                message.Msg = "设置失败," + ex.Message;
                throw ex;
            }
            return message;
        }
        #endregion

        public DataTable GetConsultTitleByID(string strID)
        {
            return DbHelper.GetDatabase().From<TB_ConsultTitle>()
                                .LeftJoin<TB_ConsultUsers>(TB_ConsultTitle._.UsersNickName == TB_ConsultUsers._.NickName)
                                .Select(TB_ConsultTitle._.All, TB_ConsultUsers._.IdentityCard, TB_ConsultUsers._.Mobile, TB_ConsultUsers._.Name, TB_ConsultUsers._.NickName,
                                        TB_ConsultUsers._.NMT_SignUpID, TB_ConsultUsers._.UserType)
                                .Where(TB_ConsultTitle._.ID == strID)
                                .ToDataTable();

        }

        public DataTable GetDicMsgTypeTable(string strName)
        {
            WhereClip wcp = new WhereClip();
            if (!string.IsNullOrEmpty(strName))
            {
                wcp = wcp.And(TB_ConsultType._.TypeName.Contain(strName));
            }
            return dal.GetTable<TB_ConsultType>(wcp, new OrderByClip(TB_ConsultType._.Sort, OrderByOperater.ASC));
        }

        public TB_ConsultType GetDicMsgTypeModel(string strConsultTypeID)
        {
            return dal.GetModel<TB_ConsultType>(TB_ConsultType._.ID == strConsultTypeID);
        }

        public JsonMessage SaveDicMsgType(string strJson)
        {
            JsonMessage message = new JsonMessage();
            try
            {
                TB_ConsultType model = strJson.ToList<TB_ConsultType>();
                dal.Save<TB_ConsultType>(model);
                message.State = "true";
                message.Msg = "保存成功!";
            }
            catch (Exception ex)
            {
                message.State = "false";
                message.Msg = "保存失败:" + ex.Message;
            }
            return message;
        }

        public DataTable GetNoticeTable(string strSignUpID)
        {
            WhereClip clip = new WhereClip();
            clip = clip.And(TB_Notice._.IsActive == "1");
            if (!string.IsNullOrEmpty(strSignUpID))
                clip = clip.And(TB_Notice._.SignUpID == strSignUpID);
            return dal.GetTable<TB_Notice>(clip, new OrderByClip(TB_Notice._.CreateTime, OrderByOperater.DESC));
        }

        public JsonMessage SaveNoticeList(string strJson)
        {
            JsonMessage message = new JsonMessage();
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                List<TB_Notice> ListNotice = strJson.ToList<List<TB_Notice>>();
                for (int i = 0; i < ListNotice.Count; i++)
                {
                    if (!ListNotice[i].IsNewModel)
                    {
                        DbHelper.GetDatabase().Update<TB_Notice>(ListNotice[i], trans);
                    }
                    else
                    {
                        DbHelper.GetDatabase().Insert<TB_Notice>(ListNotice[i], trans);
                    }
                }
                trans.Commit();
                message.State = "true";
                message.Msg = "保存成功!";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                message.State = "false";
                message.Msg = "保存失败," + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return message;
        }
    }
}
